home *** CD-ROM | disk | FTP | other *** search
- /*
- ** date.cmm
- **
- ** This CMM example file is an implementation of the DOS date
- ** command. It is meant to be a part of the CMM windows command
- ** interpretor, while at the same time showing the power of CMM.
- ** It demonstrates the use of calls into DOS at the interupt level.
- **
- */
- main(argc,argv)
- {
- // At least Print the date if it's not DOS or Windows
- if(!defined(_DOS_)&&!defined(_DOS32_)&&!defined(_WINDOWS_))
- {
- printf("%s",asctime(localtime(time())));
- exit(0);
- }
-
- /* get the current system date */
- /* use DOS call 0x2a to get the date */
- regs.ah = 0x2a;
-
- interrupt(0x21,regs, outregs);
-
- // this is the set of return values from DOS call 2a
- // Day = outregs.dl;
- // Month = outregs.dh;
- // Year = outregs.cx;
- // dayOfWeek = outregs.al;
-
- /* if the new date wasn't entered, prompt for it */
- if(argc<2)
- {
- printf("Current date is %s %02d-%02d-%d\n",GetDay(outregs.al),
- outregs.dh, outregs.dl, outregs.cx );
-
- printf("Enter new date (mm-dd-yy): ");
- gets(dateString);
- }
- else if (argc == 2)
- {
- /* try the command line argument for a new date */
- dateString = argv[1];
- }
- else
- GiveInstructionsAndExit();
-
- while(dateString[0] != '\0')
- {
- dateValid = IsDateValid(dateString);
-
- /* if the date isn't valid, let the user know */
- if(dateValid == FALSE)
- {
- dateString[0] = '\0';
- printf("\nInvalid date\n");
- printf("Enter new date (mm-dd-yy): ");
- gets(dateString);
- }
- else
- {
- /* get the pieces of the date */
- sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear);
-
- /* print the new date */
- printf("New date is %02d-%02d-%d\n",newMonth,newDay,newYear);
-
- /* use DOS call 0x2b to set the new date */
- regs.dl = newDay;
- regs.dh = newMonth;
- regs.cx = newYear;
- regs.ah = 0x2b;
-
- interrupt(0x21,regs);
-
- /* setting the string to null allows us to exit */
- dateString[0] = '\0';
- }
- }
- }
- GiveInstructionsAndExit() // Show some info about using this program
- { // and then exit this program. Do not return.
- printf("date.cmm - CEnvi date utility.\n");
- printf(" This utility requires no more than 1 argument. In general,\n");
- printf(" either the new date mm-dd-yy or no argument. If no new date is \n");
- printf(" the user will be prompted for one. \n");
- printf(" For Example:\n");
- printf(" date\n");
- printf(" date 08-05-1966\n");
- exit(EXIT_FAILURE);
-
- }
-
- /* This function is used to determine if a given date fits the format
- mm-dd-yyyy. It is used for verifying a new system date.
- On PC's a valid year is between 1980 and 2099 */
- IsDateValid(dateString)
- {
- dateValid = FALSE;
-
- /* use sscanf to get the parts of the date */
- if(sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear) == 3)
- {
- /* check the month and the year */
- if((newMonth>0)&&(newMonth<=12)&&(newYear>=1980)&&(newYear<2100))
- {
- /* now check the number of days in the month */
- switch(newMonth)
- {
- /* first the 31 day months */
- case 1: // January
- case 3: // March
- case 5: // May
- case 7: // July
- case 8: // August
- case 10: // October
- case 12: // December
- if((newDay > 0)&&(newDay <= 31))
- dateValid = TRUE;
- break;
-
- /* Now the 30 day months */
- case 4: // April
- case 6: // June
- case 9: // September
- case 11: // November
- if((newDay > 0)&&(newDay <= 30))
- dateValid = TRUE;
- break;
-
- case 2: // February
- // account for leap year
- if(((newDay > 0)&&(newDay <= 28))||
- ((newDay > 0)&&(newDay<=29)&&((newYear % 4)==0)))
- dateValid = TRUE;
- break;
- }
- }
- }
- return(dateValid);
- }
- /* convert a day represented from 0-6 to a string for display */
- GetDay(integerDay)
- {
- if(integerDay == 0)
- return("Sun");
- if(integerDay == 1)
- return("Mon");
- if(integerDay == 2)
- return("Tue");
- if(integerDay == 3)
- return("Wed");
- if(integerDay == 4)
- return("Thu");
- if(integerDay == 5)
- return("Fri");
- if(integerDay == 6)
- return("Sat");
- }
-